package com.sukaiyi.giteeideaplugin.cellrenderer;

import com.intellij.icons.AllIcons;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.sukaiyi.giteeideaplugin.model.CodeSegmentNode;
import com.sukaiyi.giteeideaplugin.utils.FileTypeUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.Objects;

public class CodeSegmentTreeCellRenderer extends ColoredTreeCellRenderer {

    @Override
    public void customizeCellRenderer(@NotNull JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        if (value instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
            Object userObject = treeNode.getUserObject();
            if (userObject == null || userObject.getClass() != CodeSegmentNode.class) {
                return;
            }
            CodeSegmentNode node = (CodeSegmentNode) userObject;
            String nodeType = node.getNodeType();
            if (Objects.equals(nodeType, "group")) {
                setIcon(AllIcons.Nodes.Folder);
                append(node.getDescription());
            } else if (Objects.equals(nodeType, "file")) {
                String name = node.getName();
                setIcon(FileTypeUtils.getIconForName(name));
                append(name);
            }
        }
    }
}